package com.yupi.yuoj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.yuoj.annotation.AuthCheck;
import com.yupi.yuoj.common.BaseResponse;
import com.yupi.yuoj.common.DeleteRequest;
import com.yupi.yuoj.common.ErrorCode;
import com.yupi.yuoj.common.ResultUtils;
import com.yupi.yuoj.constant.UserConstant;
import com.yupi.yuoj.exception.BusinessException;
import com.yupi.yuoj.exception.ThrowUtils;
import com.yupi.yuoj.model.dto.game.GameAddRequest;
import com.yupi.yuoj.model.dto.game.GameQueryRequest;
import com.yupi.yuoj.model.dto.game.GameUpdateRequest;
import com.yupi.yuoj.model.entity.Game;
import com.yupi.yuoj.model.vo.GameVO;
import com.yupi.yuoj.service.GameService;
import com.yupi.yuoj.service.UserService;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/game")
@Slf4j
public class GameController {

    @Resource
    private GameService gameService;

    @Resource
    private UserService userService;

    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> add(@RequestBody GameAddRequest addRequest, HttpServletRequest request) {
        if (addRequest == null || StringUtils.isAnyBlank(addRequest.getName())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Game entity = new Game();
        BeanUtils.copyProperties(addRequest, entity);
        if (entity.getIsActive() == null) {
            entity.setIsActive(1);
        }
        boolean result = gameService.save(entity);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(entity.getId());
    }

    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> delete(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = gameService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> update(@RequestBody GameUpdateRequest updateRequest) {
        if (updateRequest == null || updateRequest.getId() == null || updateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Game entity = new Game();
        BeanUtils.copyProperties(updateRequest, entity);
        Game old = gameService.getById(updateRequest.getId());
        ThrowUtils.throwIf(old == null, ErrorCode.NOT_FOUND_ERROR);
        boolean result = gameService.updateById(entity);
        return ResultUtils.success(result);
    }

    @GetMapping("/get")
    public BaseResponse<GameVO> get(@RequestParam("id") Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Game entity = gameService.getById(id);
        ThrowUtils.throwIf(entity == null, ErrorCode.NOT_FOUND_ERROR);
        GameVO vo = new GameVO();
        BeanUtils.copyProperties(entity, vo);
        return ResultUtils.success(vo);
    }

    @PostMapping("/list/page")
    public BaseResponse<Page<GameVO>> listByPage(@RequestBody GameQueryRequest queryRequest) {
        long current = queryRequest.getCurrent();
        long pageSize = queryRequest.getPageSize();
        QueryWrapper<Game> qw = new QueryWrapper<>();
        if (ObjectUtils.isNotEmpty(queryRequest.getId())) {
            qw.eq("id", queryRequest.getId());
        }
        if (ObjectUtils.isNotEmpty(queryRequest.getCategoryId())) {
            qw.eq("category_id", queryRequest.getCategoryId());
        }
        if (StringUtils.isNotBlank(queryRequest.getName())) {
            qw.like("name", queryRequest.getName());
        }
        if (StringUtils.isNotBlank(queryRequest.getSlug())) {
            qw.eq("slug", queryRequest.getSlug());
        }
        if (ObjectUtils.isNotEmpty(queryRequest.getIsActive())) {
            qw.eq("is_active", queryRequest.getIsActive());
        }
        Page<Game> page = gameService.page(new Page<>(current, pageSize), qw);
        Page<GameVO> voPage = new Page<>(current, pageSize, page.getTotal());
        List<GameVO> vos = page.getRecords().stream().map(item -> {
            GameVO vo = new GameVO();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        voPage.setRecords(vos);
        return ResultUtils.success(voPage);
    }
}

